<?php

/**
 * Copyright (C) 2007 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     @license http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * @author api.jmatt@gmail.com
 * @version 3.0
 */

/*
 * Converts an appropriately structured array to an XML document. Please see
 * doc.html for more information on array formatting and examples. Function
 * calls itself recursively if the array has nested arrays, a future version
 * may deal with this iteratively.
 *
 * Return: an XML fragment if array is correctly structured.
 *         false if the input is not an array or array is incorrectly 
 *         structured
 */
function ArrayToXml($inputArray) {
  
  if ( !is_array($inputArray) ) {
    return false;
  }
  
  $returnString = '';
  
  $keys = array_keys($inputArray);
  // scan through the keys to turn the array into tags
  foreach ( $keys as $key ) {
    
    $trimmedKey = trim($key);

    //check for the common case that this tag just has a value
    if ( !is_array($inputArray[$key]) ) {
      if ( is_null($inputArray[$key]) ) {
        $returnString .= '<' . $trimmedKey . ' xsi:nil="true"/>';
      } else {
        $returnString .= '<' . $trimmedKey . '>' . $inputArray[$key] . 
                            '</' . $trimmedKey . '>';
      }
      // move on to the next item in the array
      continue;
    }
    
    // open the tag
    $returnString .= '<' . $trimmedKey;

    // check to see if this tag has attributes
    if ( is_array($inputArray[$key]) && 
         array_key_exists('attributes', $inputArray[$key]) &&
         !is_null($inputArray[$key]['attributes']) ) {
      
      $attributes = $inputArray[$key]['attributes'];
      // add any attributes the tag has
      $attributeKeys = array_keys($attributes);
      foreach ( $attributeKeys as $attributeKey ) {
        $returnString .= ' ' . $attributeKey . '="' . 
                         $attributes[$attributeKey] . '"';
      } 
    }
    
    // see if this element has children or attributes and a simple value
    if ( is_array($inputArray[$key]) && 
         array_key_exists('value', $inputArray[$key]) ) {
      
      $elementValue = $inputArray[$key]['value'];
      // if the "value" element of the array is an array, this tag his children
      // otherwise, this tag has a set value and we got here because it also
      // had attributes
      if ( is_array($elementValue) ) {
        // call the function recursively
        $returnString .= '>' . arrayToXML($elementValue) . '</' . 
                         $trimmedKey . '>';
      } else {
        if ( is_null($elementValue) ) {
          $returnString .= '/>';
        } else {
          $returnString .= '>' . $elementValue . '</' . $trimmedKey . '>';
        }
      } 
    } else {
      $returnString .= '/>';
    }
    
  }
  return $returnString;
}

/**
 * Given a value which may be a boolean or a string, returns a string
 * with 'true' or 'false'
 * @param mixed $booleanVal the variable to be evaluated
 * @return string if $booleanVal would evaluate to true in PHP and $booleanVal
 * is not the string 'false', the string 'true' is returned, otherwise the
 * string 'false' is returned.
 */
function ConvertToBooleanString($booleanVal) {
  if ($booleanVal && strcmp($booleanVal, 'false') != 0) { 
    return 'true'; 
  } else { 
    return 'false'; 
  }
}
?>
